package game.module.qzmj.processor;

import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import game.common.PlayingRoleMsgProcessor;
import game.entity.PlayingRole;
import game.module.qzmj.bean.QzmjRound;
import game.module.qzmj.logic.QzmjDatamanager;
import game.session.SessionManager;
import lion.common.MsgCodeAnn;
import lion.netty4.message.MyRequestMessage;
import lion.netty4.message.RequestMessageRaw;
import lion.netty4.message.RequestProtoMessage;
import lua.LuaMessageLogin.S2ErrorCode;
import lua.LuaMessageQzmj.C2SQzmjSystemVoice;
import lua.LuaMessageQzmj.PushQzmjSystemVoice;
import lua.LuaMessageQzmj.S2CQzmjSystemVoice;
@MsgCodeAnn(msgcode = C2SQzmjSystemVoice.id, accessLimit = 500)
public class QzmjSystemVoiceProcessor extends PlayingRoleMsgProcessor {

	private static Logger logger = LoggerFactory.getLogger(QzmjSystemVoiceProcessor.class);
	
	@Override
	public void process(PlayingRole playingRole, RequestMessageRaw requestMessage) throws Exception {
		// TODO Auto-generated method stub
		C2SQzmjSystemVoice request = C2SQzmjSystemVoice.parse(requestMessage);
		byte voice_index = request.voice_index;
		int playerId = playingRole.getId();
		logger.info("qzmj voice play,playeyId={}", playerId);
		final QzmjRound mjRound = QzmjDatamanager.getInstance().getMjRoom(playerId);
		if (mjRound == null) {
			S2ErrorCode retMsg = new S2ErrorCode(201);
			playingRole.writeAndFlush(retMsg.build(playingRole.alloc()));
			return;
		}
		List<Integer> players = mjRound.getPlayerIds();
		byte play_index = (byte) players.indexOf(playerId);//获得玩家下标
		S2CQzmjSystemVoice respMsg = new S2CQzmjSystemVoice();
		playingRole.writeAndFlush(respMsg.build(playingRole.alloc())); //
		PushQzmjSystemVoice pushmsg = new PushQzmjSystemVoice();
		pushmsg.play_index = play_index;
		pushmsg.voice_index = voice_index;
		for (int i = 0; i < players.size(); i++) {
			PlayingRole pr = SessionManager.getInstance().getPlayer(players.get(i));
			if (pr != null && pr.isChannelActive()) {
				pr.writeAndFlush(pushmsg.build(pr.alloc()));
			}
		}
		
		
	}

	@Override
	public void processProto(PlayingRole playingRole, RequestProtoMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

	@Override
	public void processWebsocket(PlayingRole playingRole, MyRequestMessage request) throws Exception {
		// TODO Auto-generated method stub
		
	}

}
